﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace GetSmartIDEList.Lib.Common
{
    public class HttpGet
    {

        /// <summary>
        /// 指定Url地址使用Get 方式获取全部字符串
        /// </summary>
        /// <param name="url">请求链接地址</param>
        /// <param name="accessToken">请求的token</param>
        /// <returns></returns>
        public static string Get(string url, string accessToken = null)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            //header 'Content-Type: application/json;charset=UTF-8

            req.Method = "GET";
            req.Headers["Content-Type"] = "application/json;charset=UTF-8";

            if (!string.IsNullOrEmpty(accessToken))
                req.Headers["Authorization"] = "Bearer " + accessToken;

            try
            {
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();
                try
                {
                    //获取内容
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                finally
                {
                    stream.Close();
                }
            }
            catch (Exception ex)
            {

            }

            return result;
        }
    }
}
